package com.test;


import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import com.westcom.basicstruct.GetCardListStruct;

public class MakeJson {
	JSONObject jsonObject;
	public JSONObject MakeLoginJson(String MailAddress, String MD5PassWord)
	{
		jsonObject = new JSONObject();
		
		try {
			jsonObject.put("mailAddress", MailAddress);
			jsonObject.put("password", MD5PassWord);
			jsonObject.put("needToken", "0");
			return jsonObject;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	//"0" not share, "1" share
	public JSONObject MakeCreateSheetJson(String sheetName, String shareFlg)
	{
		jsonObject = new JSONObject();
		
		try {
			jsonObject.put("sheetName", sheetName);
			jsonObject.put("shareFlg", shareFlg);
			return jsonObject;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	public JSONObject MakeCreateCategoryJson(int SheetId, String categoryName, String dispCol, int colorIdx)
	{
		jsonObject = new JSONObject();
		
		try {
			jsonObject.put("sheetId", SheetId);
			jsonObject.put("categoryName", categoryName);
			jsonObject.put("dispCol", dispCol);
			jsonObject.put("colorIdx", colorIdx);
			return jsonObject;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public JSONObject MakeCategoryListJson(int SheetId)
	{
		jsonObject = new JSONObject();
		
		try {
			jsonObject.put("sheetId", SheetId);
			return jsonObject;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public JSONObject MakeCreateCardJson(int categoryId, int[] resourceIds)
	{
		jsonObject = new JSONObject();
		
		if(resourceIds.length == 0)
			return null;
		
		try {
			jsonObject.put("categoryId", categoryId);
			String para = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><card><resource id=\"" + resourceIds[0] +"\" /><br></card>";
			jsonObject.put("contents", para);
			String resourceids = "[";
			for(int i = 0; i < resourceIds.length; i++)
			{
				resourceids = resourceids + resourceIds[i] + ",";
			}
			resourceids = resourceids.substring(0, resourceids.length() - 1);
			resourceids = resourceids + "]";
			jsonObject.put("resourceIds", resourceids);
			return jsonObject;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	public JSONObject MakeCategoryStatisticJson(int[] categoryIds)
	{
		jsonObject = new JSONObject();		
		try {
			String categoryids = "[";
			for(int i = 0; i < categoryIds.length; i++)
			{
				categoryids = categoryids + categoryIds[i] + ",";
			}
			categoryids = categoryids.substring(0, categoryids.length() - 1);
			categoryids = categoryids + "]";
			jsonObject.put("categoryIds", categoryids);
			return jsonObject;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	public JSONObject MakeCardListJson(GetCardListStruct[] categories)
	{
		jsonObject = new JSONObject();		
		try {
			StringBuilder builder=new StringBuilder();
			ArrayList<String> folksData=new ArrayList<String>();
			JSONArray array=new JSONArray();
			for(int i=0;i<categories.length;i++){
				JSONObject jsonObject=new JSONObject();
				jsonObject.put("categoryId", categories[i].categoryId);
				jsonObject.put("count", categories[i].count);
				jsonObject.put("start", categories[i].start);
				folksData.add(jsonObject.toString());
				array.put(jsonObject);
			}
//			JSONArray jsonArray=new JSONArray(folksData);
			int len =	array.length();
			jsonObject=new JSONObject(new JSONStringer().object().key("categories").value(array).endObject().toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return jsonObject;
	}
}
